<template>
    <div class="template-list" v-loading="loading">
        <div class="template-option-box">
            <div class="input-box">
                <el-button type="primary" class="btn" style="margin-left: 0px;" @click="goDetail()">新建</el-button>
            </div>
        </div>
        <div class="table-box">
            <p class="table-hint">*注意：上架状态的礼包，到达发放时间将自动发放；</p>
            <el-table :data="tableData" ref="table" border style="width: 100%" :span-method="tableMerge">
                <el-table-column width="50" prop="activityId" label="ID"></el-table-column>
                <el-table-column min-width="120" label="关联游戏">
                    <template slot-scope="scope">
                        <span class="table-btn" @click="goGameDetail(scope.row.gameId)">{{ scope.row.gameName || '-' }}</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" prop="maxReward" label="发放对象">
                    <template slot-scope="scope">
                        <span>{{ userList[scope.row.giveUser] }}</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" label="发放渠道">
                    <template>
                        <div>游戏时光宝箱</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="180" prop="giveTime" label="发放时间"></el-table-column>
                <el-table-column min-width="80" prop="getTime" label="领取次数"></el-table-column>
                <el-table-column min-width="150" prop="rewardDesc" label="礼包内容"></el-table-column>
                <el-table-column min-width="150" label="领取数/礼包码总数">
                    <template slot-scope="scope">
                        <div>{{ scope.row.exchangeCount }} / {{ scope.row.rewardCount }}</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="150" prop="endTime" label="截止日期">
                    <template slot-scope="scope">
                        <div>{{ initTime(scope.row.endTime) }}</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="80" label="状态">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status === 0" style="color:#F56C6C;">下架</div>
                        <div v-else style="color: #67C23A;">已上架</div>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status === 1">
                            <el-button @click="changeStatus(scope.row, 0)" type="text" size="small">下架</el-button>
                        </div>
                        <div v-if="scope.row.status === 0">
                            <el-button @click="changeStatus(scope.row, 1)" type="text" size="small">上架</el-button>
                        </div>
                        <div>
                            <el-button @click="goDetail(scope.row.activityId)" type="text" size="small">编辑</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
export default {
    data () {
        return {
            tableData: [],
            userList: ['全平台', '同游戏用户', '同游戏新用户', '同游戏老用户', '新用户', '老用户'],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            loading: false
        }
    },
    created () {
        this.getList(this.pageNum)
    },
    methods: {
        // 获取列表
        async getList (pageNum) {
            if (this.loading) return
            this.pageNum = pageNum
            this.loading = true
            let params = {
                pageNum: pageNum,
                pageSize: this.pageSize
            }
            let res = await this.$http.post('/lottrey/selectGiftList', params)
            if (res.code === '0') {
                this.tableData = this.format(res.data)
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },
        format (data) {
            let table = []
            let activityList = []
            let tableIndex = 0
            for (let i = 0; i < data.length; i++) {
                activityList = data[i].boxActivityRewardVoList
                if (activityList.length > 0) {
                    if (activityList.length < 2) { // 如果用户抽奖数据少于两条，则直接插入
                        table.push(data[i])
                        table[tableIndex].rewardDesc = activityList[0].rewardDesc || '-'
                        table[tableIndex].rewardCount = activityList[0].rewardCount || 0
                        table[tableIndex].exchangeCount = activityList[0].exchangeCount || 0
                        table[tableIndex].endTime = activityList[0].endTime
                        table[tableIndex]['tableIndex'] = i + 1
                        tableIndex++
                    } else { // 抽奖数据多余1条时，将每一条循环到table中，表格合并使用
                        let activityObj = {}
                        for (let j = 0; j < activityList.length; j++) {
                            table.push(data[i])
                            activityObj = {
                                rewardDesc: activityList[j].rewardDesc || '-',
                                rewardCount: activityList[j].rewardCount || 0,
                                exchangeCount: activityList[j].exchangeCount || 0,
                                endTime: activityList[j].endTime,
                                merge: 0
                            }
                            table[tableIndex] = JSON.parse(JSON.stringify(Object.assign(table[tableIndex], activityObj)))
                            if (j === 0) { // 添加合并单元格标识
                                table[tableIndex].merge = activityList.length
                            }
                            table[tableIndex]['tableIndex'] = i + 1
                            tableIndex++
                        }
                    }
                } else {
                    table.push(data[i])
                    table[tableIndex]['tableIndex'] = i + 1
                    tableIndex++
                }
            }
            return table
        },
        tableMerge ({ row, column, rowIndex, columnIndex }) {
            if ((row.merge >= 0 && columnIndex < 5) || columnIndex > 8) {
                if (row.merge === 0) {
                    return {
                        rowspan: 0,
                        colspan: 0
                    }
                } else if (!row.merge) {
                    return {
                        rowspan: 1,
                        colspan: 1
                    }
                } else {
                    return {
                        rowspan: row.merge,
                        colspan: 1
                    }
                }
            }
        },
        initTime (time) {
            if (time) {
                return formatDate(new Date(time), 'YYYY-MM-DD')
            } else {
                return '-'
            }
        },
        changeStatus (row, status) {
            console.log(row)
            this.$http.post('/lottrey/updateBoxActivityStatus', {
                activityId: row.activityId,
                status
            }).then(res => {
                if (res.code === '0') {
                    let message = status === 0 ? '下架成功' : '上架成功'
                    this.$message({
                        type: 'success',
                        message
                    })
                    row.status = status
                }
            })
        },
        goDetail (id) {
            console.log(id)
            if (id) {
                this.$router.push({
                    path: '/task/giftDetail',
                    query: {
                        id
                    }
                })
            } else {
                this.$router.push('/task/giftDetail')
            }
        },
        goGameDetail (id) {
            const { href } = this.$router.resolve({
                path: '/game/gameDetail',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },
        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList(1)
        },
        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        }
    }
}
</script>

<style lang="scss" scoped>
    .template-list {
        .template-option-box {
            max-width: 800px;
        }
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 20px;
            }
            .btn {
                margin-top: 10px;
                margin-left: 10px;
            }
        }

        .table-box {
            margin: 20px 0;
            .table-hint {
                margin-bottom: 10px;
                font-size: 14px;
                color: #f56c6c;
            }
            .cover {
                width: 80px;
                height: 80px;
                > img {
                    width: 100%;
                    height: 100%;
                }
            }
            .desc {
                overflow: hidden;
                line-height: 20px;
                -webkit-line-clamp: 5;
                -webkit-box-orient: vertical;
                display: -webkit-box;
                height: 100px;
            }
            .user-icon {
                width: 20px;
                height: 20px;
                border-radius: 50%;
                margin-right: 5px;
                vertical-align: top;
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
            .point {
                display:inline-block;
                width:10px;
                height:10px;
                border-radius: 50%;
                background-color:#67C23A;
                margin-right:3px;
            }
            .red-point {
                background-color: #F56C6C;
            }
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
